package com.example.mp.controller;



import cn.hutool.core.bean.BeanUtil;
import com.example.mp.domain.KaoTi;
import com.example.mp.dto.SaveKaoTiDTO;
import com.example.mp.service.KaoTiService;
import com.example.mp.vo.KaoTiVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/MP")
public class KaoTIController {
    @Autowired
    private KaoTiService kaoTiService;

//    @PostMapping ( "/save")
//    public void save(@RequestBody SaveKaoTiDTO dto){
//        KaoTi kaoti = new KaoTi();
//        BeanUtil.copyProperties(dto,kaoti);
//        KaoTiService.save(kaoti);
//    }

//    @GetMapping("/deleteByid/{id}")
//    public void deleteByid(@PathVariable("id") Integer id){
////        KaoTiService.removeByid(id);
//    }

//    @GetMapping("/findAll")
//    public List<KaoTiVO>  findAll(){
//        List<KaoTiVO> list = KaoTiService.findAll;
//        List<KaoTiVO> result = (List<KaoTiVO>) BeanUtil.copyProperties(list, KaoTiVO.class);
//        return result;
//    }

//    @GetMapping("/update")
//    public void update(@RequestBody UpdateDTO dto){
//        KaoTi kaoTi = new KaoTi();
//        BeanUtil.copyProperties(dto,kaoTi);
//        KaoTiService.UpdateByid(kaoTi);
//    }

    @GetMapping("/findByid/{id}")
    public KaoTi findByid(@PathVariable("id") Integer id) {
        return kaoTiService.getById(id);
    }

    @GetMapping("/deleteByid/{id}")
    public void deleteByid(@PathVariable("id") Integer id) {
        kaoTiService.removeById(id);
    }

    @PostMapping("/save")
    public void save(@RequestBody SaveKaoTiDTO dto) {
        KaoTi kaoti = new KaoTi();
        BeanUtil.copyProperties(dto, kaoti);
        kaoTiService.saveOrUpdate(kaoti);
    }

    @GetMapping("/findAll")
    public List<KaoTiVO> findAll() {
        List<KaoTi> list = kaoTiService.list();
        List<KaoTiVO> result =BeanUtil.copyToList(list,KaoTiVO.class);
        return result;
    }

//    @GetMapping("/findAll")
//    public List<KaoTi> findAll() {
//        kaoTiService.list();
//        List<KaoTiVO> result = BeanUtil.copyProperties(list, KaoTiVO.class);
//    }
}

